package com.example.yiwei.crm.view.activity.healthActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.healthEducationDao;
import com.example.yiwei.crm.dao.managerDao;
import com.example.yiwei.crm.dao_impl.healthEducationDaoImpl;
import com.example.yiwei.crm.dao_impl.managerDaoImpl;
import com.example.yiwei.crm.entity.ApointmentEntity;
import com.example.yiwei.crm.entity.BZFLEntity;
import com.example.yiwei.crm.entity.DepartmentEntity;
import com.example.yiwei.crm.entity.managerEntity;
import com.example.yiwei.crm.json.YYSpinnerJson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.util.ZhengZhe;
import com.example.yiwei.crm.view.activity.coordinationActivity.CoordingAppointmentSearchActivity;
import com.example.yiwei.crm.view.activity.customerActivity.CustomerAddActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class HealthAddClueActivity extends AppCompatActivity {
private ImageView img_back,img_commit;
    private EditText edit_clue_name,edit_age,edit_contact,edit_phone,edit_additional;
    private Spinner spinner_disease_category,spinner_disease,spinner_qdfl,spinner_qdly,spinner_department,spinner_manager;
    private RadioGroup rg_sex;
    private RadioButton rb_men,rb_women;
    int manager_id,sex,disease_category_id;
    private List<BZFLEntity> list4;
    private List<BZFLEntity> list3;
    List<managerEntity> managerList = new ArrayList<>();
    List<DepartmentEntity> departmentList = new ArrayList<>();
    private List<String> name_list=new ArrayList<>();
    private ArrayAdapter adapter1,adapter2;
    SharedPreferences sp;
    String managerName,result;
    int department_id;
    String hospital_name,hospital_bianma,department_name,department_bianma,disease_category,disease_bianma,qdfl,qdly;
    int qdfl_id,qdly_id;
    List<String> hospital_list,department_list,disease_category_list,disease_list,qdfl_list,qdly_list,jd_list;
    private ArrayAdapter adapter ,adapter3,adapter4,adapter5,adapter6,adapter7,adapter8;
    private ApointmentEntity apo;
    private int disease_id;
    private String disease;
    private List<BZFLEntity> list5;
    private List<BZFLEntity> list6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health_add_clue);
        spinner_manager= (Spinner) findViewById(R.id.health_add_clue_spinner_manager);
        img_back= (ImageView) findViewById(R.id.customer_add_img_back);
        img_commit= (ImageView) findViewById(R.id.customer_add_img_commit);
        edit_clue_name= (EditText) findViewById(R.id.health_add_clue_edit_name);
        edit_age= (EditText) findViewById(R.id.health_add_clue_edit_age);
        edit_contact= (EditText) findViewById(R.id.health_add_clue_contact_name);
        edit_phone= (EditText) findViewById(R.id.health_add_clue_edit_mobile_phone);
        edit_additional= (EditText) findViewById(R.id.health_add_clue_edit_additional);
        spinner_department= (Spinner) findViewById(R.id.health_add_clue_spinner_department);
        spinner_disease_category= (Spinner) findViewById(R.id.health_add_clue_spinner_disease_category);
        spinner_disease= (Spinner) findViewById(R.id.health_add_clue_spinner_disease);
        spinner_qdfl= (Spinner) findViewById(R.id.health_add_clue_spinner_qdfl);
        spinner_qdly= (Spinner) findViewById(R.id.health_add_clue_spinner_qdly);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        rg_sex= (RadioGroup) findViewById(R.id.customer_add_rg_sex);
        rb_men= (RadioButton) findViewById(R.id.customer_add_rb_men);
        rb_women= (RadioButton) findViewById(R.id.customer_add_rb_women);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        rg_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==rb_men.getId()){
                    sex=1;
                }else{
                    sex=2;
                }
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
                    object.put("admin",sp.getString("admin",null));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                managerDao manaDao=new managerDaoImpl();
                managerList=manaDao.queryAllManager(baseClass.query_manager_url,json);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i2=0;i2<managerList.size();i2++){
                            name_list.add( managerList.get(i2).getManager_name());
                        }
                        adapter1 = new ArrayAdapter(HealthAddClueActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner, name_list);
                        adapter1.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        spinner_manager.setAdapter(adapter1);
                        spinner_manager.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                managerName=managerList.get(i).getManager_name();
                                manager_id = managerList.get(i).getManager_id();
                            }
                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {

                            }
                        });
                        if ("updata".equals(getIntent().getStringExtra("updata"))){
                            for (int i = 0;i<name_list.size();i++){
//                                if (name_list.get(i).equals(customer.getClient_manager_name())){
//                                    spinner_manager.setSelection(i);
//                                }
                            }
                        }
                    }
                });
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc1 = new SC();
                JSONObject object3 = sc1.storage(sp);
                String json2 = object3.toString();
                String result2 = NetWorkUtils.getCount(baseClass.find_BZFL_URL,json2);
                list3 = YYSpinnerJson.getBZFL(result2);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        disease_category_list= new ArrayList<>();
                        for (int i2=0;i2<list3.size();i2++){
                            disease_category_list.add( list3.get(i2).getName());
                        }
                        adapter3 = new ArrayAdapter(HealthAddClueActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, disease_category_list);
                        adapter3.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        spinner_disease_category.setAdapter(adapter3);
                        spinner_disease_category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                if (position>0){
                                    disease_category=list3.get(position-1).getName();
                                    disease_category_id = list3.get(position-1).getId();
                                }else {
                                    disease_category_id=0;
                                }
                                if ("updata".equals(getIntent().getStringExtra("TAG"))){
                                    for (int i = 0;i<disease_category_list.size();i++){
                                        if (disease_category_list.get(i).equals(apo.getDisease_category())){
                                            spinner_disease_category.setSelection(i);
                                        }
                                    }
                                }
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        SC sc = new SC();
                                        JSONObject object2 = sc.storage(sp);
                                        try {
                                            object2.put("AKB01",disease_category_id);
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        String json = object2.toString();
                                        String result = NetWorkUtils.getCount(baseClass.find_BZ_URL, json);
                                        list4 = YYSpinnerJson.getBZ(result);
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                disease_list = new ArrayList<String>();
                                                for (int i2=0;i2<list4.size();i2++){
                                                    disease_list.add( list4.get(i2).getName());
                                                }
                                                adapter4 = new ArrayAdapter(HealthAddClueActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, disease_list);
                                                adapter4.setDropDownViewResource(R.layout.spinner_dropdown_style);
                                                spinner_disease.setAdapter(adapter4);
                                                spinner_disease.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                                    @Override
                                                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                                        if (position>0){
                                                            disease=list4.get(position-1).getName();
                                                            disease_id = list4.get(position-1).getId();
                                                        }else {
                                                            disease_id=0;
                                                        }
                                                        if ("updata".equals(getIntent().getStringExtra("TAG"))){
                                                            for (int i = 0;i<disease_list.size();i++){
                                                                if (disease_list.get(i).equals(apo.getDisease())){
                                                                    spinner_disease.setSelection(i);
                                                                }
                                                            }
                                                        }
                                                    }
                                                    @Override
                                                    public void onNothingSelected(AdapterView<?> parent) {

                                                    }
                                                });
                                            }
                                        });
                                    }
                                }).start();
                            }
                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                    }
                });
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc1 = new SC();
                JSONObject object3 = sc1.storage(sp);
                String json2 = object3.toString();
                String result2 = NetWorkUtils.getCount(baseClass.find_QDFL_URL,json2);
                list5 = YYSpinnerJson.getQDFL(result2);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        qdfl_list= new ArrayList<>();
                        for (int i2=0;i2<list5.size();i2++){
                            qdfl_list.add(list5.get(i2).getName());
                        }
                        adapter5 = new ArrayAdapter(HealthAddClueActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner,qdfl_list);
                        adapter5.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        spinner_qdfl.setAdapter(adapter5);
                        spinner_qdfl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                if (position>0){
                                    qdfl=list5.get(position-1).getName();
                                    qdfl_id = list5.get(position-1).getId();
                                }else {
                                    qdfl_id=0;
                                }
                                if ("updata".equals(getIntent().getStringExtra("TAG"))){
                                    for (int i = 0;i<qdfl_list.size();i++){
                                        if (qdfl_list.get(i).equals(apo.getClue_channel_classification())){
                                            spinner_qdfl.setSelection(i);
                                        }
                                    }
                                }
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        SC sc = new SC();
                                        JSONObject object2 = sc.storage(sp);
                                        try {
                                            object2.put("AKC01",qdfl_id);
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        String json = object2.toString();
                                        String result = NetWorkUtils.getCount(baseClass.find_QD_URL, json);
                                        list6 = YYSpinnerJson.getQDLY(result);

                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                qdly_list = new ArrayList<String>();
                                                for (int i2=0;i2<list6.size();i2++){
                                                    qdly_list.add( list6.get(i2).getName());
                                                }
                                                adapter6 = new ArrayAdapter(HealthAddClueActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, qdly_list);
                                                adapter6.setDropDownViewResource(R.layout.spinner_dropdown_style);
                                                spinner_qdly.setAdapter(adapter6);
                                                spinner_qdly.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                                    @Override
                                                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                                        if (position>0){
                                                            qdly=list6.get(position-1).getName();
                                                            qdly_id = list6.get(position-1).getId();
                                                        }else {
                                                            qdly_id= 0;
                                                        }

                                                    }
                                                    @Override
                                                    public void onNothingSelected(AdapterView<?> parent) {

                                                    }
                                                });
                                            }
                                        });
                                    }
                                }).start();
                            }
                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                    }                });
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
                    object.put("admin",sp.getString("admin",null));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                healthEducationDao healthEducationDao=new healthEducationDaoImpl();
                departmentList=healthEducationDao.queryAllDepartmentMessage(baseClass.find_dock_bm_URL,json);
                department_list=new ArrayList<String>();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i2=0;i2<departmentList.size();i2++){
                            department_list.add( departmentList.get(i2).getDepartment());
                        }
                        adapter2 = new ArrayAdapter(HealthAddClueActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner, department_list);
                        adapter2.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        spinner_department.setAdapter(adapter2);
                        spinner_department.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                department_name=departmentList.get(i).getDepartment();
                                department_id = departmentList.get(i).getDepartment_id();
                            }
                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {

                            }
                        });
                        if ("updata".equals(getIntent().getStringExtra("updata"))){
                            for (int i = 0;i<department_list.size();i++){
//                                if (name_list.get(i).equals(customer.getClient_manager_name())){
//                                    spinner_manager.setSelection(i);
//                                }
                            }
                        }
                    }
                });
            }
        }).start();
        img_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean b = ZhengZhe.phone(edit_phone.getText().toString());
                if (!b){
                    img_commit.setVisibility(View.VISIBLE);
                    Toast.makeText(HealthAddClueActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                }else if (edit_age.getText().toString().equals("")){
                    img_commit.setVisibility(View.VISIBLE);
                    Toast.makeText(HealthAddClueActivity.this, "请输入年龄", Toast.LENGTH_SHORT).show();
                }  else{
                    img_commit.setVisibility(View.GONE);
                    new  Thread(new Runnable() {
                        @Override
                        public void run() {
                            SC sc1 = new SC();
                            JSONObject object = sc1.storage(sp);
                            try {
                                object.put("addoredit", "add");
                                object.put("XAA02",manager_id);
                                object.put("XAA03",edit_clue_name.getText().toString());
                                object.put("XAA04",sex);
                                object.put("XAA05",Integer.valueOf(edit_age.getText().toString()));
                                object.put("AKB01",disease_category_id);
                                object.put("AKM01",disease_id);
                                object.put("XAA06",edit_contact.getText().toString());
                                object.put("XAA08",edit_phone.getText().toString());
                                object.put("AKC01",qdfl_id);
                                object.put("AKD01",qdly_id);
                                object.put("XAA13",department_id);
                                object.put("XAA16",edit_additional.getText().toString());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            String json=object.toString();
                            result=NetWorkUtils.getCount(baseClass.add_clue_url,json);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        JSONObject json1=new JSONObject(result);
                                        String boolen=json1.getString("ifsuccess");
                                        if ("true".equals(boolen)){
                                            Toast.makeText(HealthAddClueActivity.this, "新增线索成功", Toast.LENGTH_SHORT).show();
                                            finish();
                                        }else{
                                            Toast.makeText(HealthAddClueActivity.this, "新增线索失败", Toast.LENGTH_SHORT).show();
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });



                        }
                    }).start();
                }
            }
        });

    }
}
